﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;

namespace Net
{
    class NetCallback
    {
        public const int Block = 0;
        public const int WaitBoolCallback = 1;
        public const int WaitUpdateInfo = 2;
        public const int WaitString = 3;
        public const int WaitDataSet = 4;
        public const int WaitDataTable = 5;
        public const int WaitInt = 6;

        private byte packageIdentifier;
        public byte PackageIdentifier
        {
            get { return packageIdentifier; }
            set { packageIdentifier = value; }
        }

        private bool removeRequire;
        public bool RemoveRequire
        {
            get { return removeRequire; }
            set { removeRequire = value; }
        }

        private int callbackType;
        public int CallbackType
        {
            get { return callbackType; }
            set { callbackType = value; }
        }

        private int state;
        public int State
        {
            get { return state; }
            set { state = value; }
        }

        private object asyncObject;
        public object AsyncObject
        {
            get { return asyncObject; }
            set { asyncObject = value; }
        }

        private ManualResetEvent dataReach;
        public ManualResetEvent DataReach
        {
            get { return dataReach; }
            set { dataReach = value; }
        }

        public BoolCallback boolCallback;
        public UpdateInfoCallback updateInfoCallback;
        public IntCallback intCallback;
        public StringCallback stringCallback;
        public UpdateDataReachCallback updateDataReachCallback;
        public DataSetCallback dataSetCallback;
        public DataTableCallback dataTableCallback;

        public void Execute()
        {
            switch (state)
            {
                case Block:
                    dataReach.Set();
                    break;
                case WaitBoolCallback:
                    boolCallback((bool)AsyncObject);
                    break;
                case WaitUpdateInfo:
                    if (((UpdateInfo)asyncObject).updateIndex == -1 && updateInfoCallback != null)
                    {
                        updateInfoCallback(((UpdateInfo)asyncObject).updateFileName, ((UpdateInfo)asyncObject).updateFileLength);
                    }
                    else if (intCallback != null)
                    {
                        updateDataReachCallback(((UpdateInfo)asyncObject).updateIndex, ((UpdateInfo)asyncObject).dataLength);
                    }
                    break;
                case WaitString:
                    stringCallback((string)asyncObject);
                    break;
                case WaitDataSet:
                    dataSetCallback((DataSet)asyncObject);
                    break;
                case WaitDataTable:
                    dataTableCallback((DataTable)asyncObject);
                    break;
                case WaitInt:
                    intCallback((int)asyncObject);
                    break;
                default:
                    break;
            }
        }
    }
}
